package com.example.myviewgroup;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Arrays;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {

    private final String[] mTextArray = new String[]{"天猫精灵", "充电台灯", "睡衣", "手表",
            "创意水杯", "夏天T恤男", "Galaxy红轴至尊版机械键盘",
            "计算机组成原理", "学霸笔记本", "可口可乐", "跑步机",
            "旅行箱", "竹浆卫生纸", "吹风机", "洗面奶", "窗帘"};
    private MyViewGroupAdapter mGroupAdapter;
    private AddFoldView mFlexBoxLayout;
    private LinkedList<String> mStringList = new LinkedList<>();
    private static final String TAG = "Main";
    private TextView mAddView;
    private TextView mEditView;
    private TextView mDoneView;
    private TextView mClearView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        initClickListener();
    }

    private void initData(){
        mStringList.addAll(Arrays.asList(mTextArray));
    }

    private void initClickListener(){
        mGroupAdapter.setOnDeleteItemClickListener(position -> {
            mStringList.remove(position);
            mGroupAdapter.setData(mStringList);
        });

        mAddView.setOnClickListener(view -> {
            if (mStringList.size() == 20){
                mStringList.removeFirst();
            }
            mStringList.add(mTextArray[(int) (Math.random()*16)]);
            mFlexBoxLayout.setIsFold(true);
            mGroupAdapter.setData(mStringList);
            //添加时，设置为添加状态true
            mFlexBoxLayout.mIsAdd = true;
            mGroupAdapter.notifyDataChanged();
        });

        mEditView.setOnClickListener(view -> {
            isVisible(true);
            mFlexBoxLayout.setIsFold(false);
            mGroupAdapter.notifyDataChanged();
        });

        mDoneView.setOnClickListener(view -> {
            isVisible(false);
            mFlexBoxLayout.setIsFold(true);
            mGroupAdapter.notifyDataChanged();
        });

        mClearView.setOnClickListener(view -> {
            isVisible(false);
            mStringList = new LinkedList<>();
            mGroupAdapter.setData(new LinkedList<>());
            mGroupAdapter.notifyDataChanged();
        });
    }

    private void initView(){
        mAddView = findViewById(R.id.add_view);
        mEditView = findViewById(R.id.edit_view);
        mDoneView = findViewById(R.id.done_view);
        mClearView = findViewById(R.id.remove_all);

        mFlexBoxLayout = findViewById(R.id.my_view_group);
        mGroupAdapter = new MyViewGroupAdapter(this);
        mGroupAdapter.setData(mStringList);
        mFlexBoxLayout.setMyAdapter(mGroupAdapter);
    }


    private void isVisible(boolean flag){
        if (flag){
            mEditView.setVisibility(View.GONE);
            mDoneView.setVisibility(View.VISIBLE);
            mClearView.setVisibility(View.VISIBLE);
        } else {
            mEditView.setVisibility(View.VISIBLE);
            mDoneView.setVisibility(View.GONE);
            mClearView.setVisibility(View.GONE);
        }
        mGroupAdapter.mIsEdit = flag;
        mGroupAdapter.mIsDelete = flag;
    }
}